import ttkbootstrap as ttk
import tkinter as tk
from ttkbootstrap.constants import *
from operations import calculate, get_history
import tkinter.messagebox as messagebox
    
def main():
    messagebox.showwarning('提示', '请不要关闭命令行窗口，否则程序将停止运行。')
    root = ttk.Window(themename="morph")
    root.title('计算器Plus')
    root.geometry('600x800')  # 增大主窗口尺寸
    
    style = ttk.Style()
    style.configure('TButton', font=('Microsoft YaHei', 12), padding=6)
     
    # 输入框
    entry = ttk.Entry(root, width=30, bootstyle=SECONDARY)
    entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10, sticky="nsew")
    
    # 按钮布局
    buttons = [
        ('7', 1,0), ('8',1,1), ('9',1,2), ('/',1,3, DANGER),
        ('4', 2,0), ('5',2,1), ('6',2,2), ('*',2,3, DANGER),
        ('1', 3,0), ('2',3,1), ('3',3,2), ('-',3,3, DANGER),
        ('0', 4,0), ('.',4,1), ('=',4,2, SUCCESS), ('+',4,3, DANGER),
        ('(', 7,0, WARNING), (')', 7,1, WARNING),
        ('sin',5,0, INFO), ('cos',5,1, INFO), ('tan',5,2, INFO), ('log',5,3, INFO),
        ('π',6,0, WARNING), ('e',6,1, WARNING), ('√',6,2, WARNING), ('^',6,3, WARNING),
        ('历史',7,2, LIGHT)
    ]
    
    # 创建按钮
    for btn_info in buttons:
        text, row, col = btn_info[:3]
        bootstyle = btn_info[3] if len(btn_info)>3 else PRIMARY
        btn = ttk.Button(root, text=text, bootstyle=bootstyle,
                        command=lambda t=text: button_click(t))
        btn.grid(row=row, column=col, padx=2, pady=2, sticky="nsew")

    def button_click(value):
        if value == '=':
            try:
                expression = entry.get()
                result = calculate(expression)
                entry.delete(0, tk.END)
                entry.insert(0, str(result))
            except Exception as e:
                entry.delete(0, tk.END)
                entry.insert(0, "错误")
        elif value == '历史':
            show_history()
        else:
            current = entry.get()
            entry.delete(0, tk.END)
            entry.insert(0, current + value)

    def show_history():
        history_win = ttk.Toplevel(root)
        history_win.title("计算历史")
        history_win.geometry('500x600')  # 增大历史窗口尺寸
        
        frame = ttk.Frame(history_win)
        frame.pack(fill=BOTH, expand=True, padx=15, pady=15)
        
        scrollbar = ttk.Scrollbar(frame)
        scrollbar.pack(side=RIGHT, fill=Y)
        
        tree = ttk.Treeview(frame, columns=("history",), show="headings",
                          yscrollcommand=scrollbar.set)
        tree.heading("history", text="最近10条记录")
        tree.pack(fill=BOTH, expand=True)
        
        for record in get_history():
            tree.insert("", END, values=(record,))
        
        scrollbar.config(command=tree.yview)

    # 设置响应式布局
    for i in range(8):
        root.grid_rowconfigure(i, weight=1)
    for i in range(4):
        root.grid_columnconfigure(i, weight=1)

    root.mainloop()

if __name__ == "__main__":
    main()